package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IProductInfoPackageService;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.pojo.ProductInfoPackagePojo;
import com.wondertek.poms.service.vo.ProductInfoPackageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
/**
 * <p><b>ProductInfoPackageController Description:</b> 二级产品信息 Controller层实现</p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-19 15:56:40</p>
 */
@Api(value = "ProductInfoPackageController", description = "二级产品 接口")
@RestController
@RequestMapping(value = "/product-info-package")
public class ProductInfoPackageController extends BaseController {

	@Autowired
	private IProductInfoPackageService productInfoPackageService;

	@ApiOperation(value = "节目 列表", notes = "节目 列表")
	@GetMapping(value = "/list")
	@ResponseBody
	public ResultBean list(
			ProductInfoPackageVo productInfoPackageVo,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "20") int rows,
			@RequestParam(defaultValue = "lastUpdateDate") String sidx,
			@RequestParam(defaultValue = "desc") String sord) {
		ProductInfoPackagePojo productInfoPackagePojo = new ProductInfoPackagePojo();
		BeanUtils.copyProperties(productInfoPackageVo, productInfoPackagePojo);
		Page<ProductInfoPackagePojo> pages = productInfoPackageService
				.findAll(productInfoPackagePojo, page, rows, sord, sidx);

		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation(value = "二级产品信息 列表", notes = "二级产品信息 列表")
	@GetMapping(value = "/list-by-id")
	@ResponseBody
	public ResultBean list(String firstProductInfoPackageId) {
//		ProductInfoPackagePojo productInfoPackagePojo = new ProductInfoPackagePojo();
//		BeanUtils.copyProperties(productInfoPackageVo, productInfoPackagePojo);
//		Page<ProductInfoPackagePojo> pages = productInfoPackageService
//				.findAll(productInfoPackagePojo, page, rows, sord, sidx);

//		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
		return null;
	}

	@ApiOperation(value = "二级产品信息 新增", notes = "二级产品信息 新增")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productInfoPackageName", required = true ,value = "二级产品名"),
			@ApiImplicitParam(name = "firstProductInfoPackageId", required = true,value = "一级产品id"),
			@ApiImplicitParam(name = "type", required = true,value = "类型:大屏:1，小屏:2，VR:3")
	})
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean add(@RequestBody ProductInfoPackageVo productInfoPackageVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(productInfoPackageVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}

		if (isRepeat(productInfoPackageVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));

		ProductInfoPackage productInfoPackage = new ProductInfoPackage();
		BeanUtils.copyProperties(productInfoPackageVo, productInfoPackage);

		productInfoPackage.setFirstProductInfoPackageId(productInfoPackageVo.getFirstProductInfoPackageId());

		ProductInfoPackage.putTimeAndUser(productInfoPackage);
		productInfoPackageService.save(productInfoPackage);
		return ResultBean.ok(productInfoPackage);
	}

	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断二级产品信息 名称是否重复</p>
	 *
	 * @param productInfoPackageVo
	 * @author douzi
	 */
	private boolean isRepeat(ProductInfoPackageVo productInfoPackageVo) {
		ProductInfoPackage temp = new ProductInfoPackage();
		temp.setProductInfoPackageName(productInfoPackageVo.getProductInfoPackageName());
		Optional<ProductInfoPackage> productInfoPackageOpt = productInfoPackageService.findOne(Example.of(temp));
		if (productInfoPackageOpt.isPresent()) {
			ProductInfoPackage productInfoPackage = productInfoPackageOpt.get();
			if (productInfoPackageVo.getProductInfoPackageId() != null && productInfoPackageVo.getProductInfoPackageId() != productInfoPackage.getProductInfoPackageId())
				return true;
			else if (productInfoPackageVo.getProductInfoPackageId() == null)
				return true;
		}
		return false;
	}

}
